package org.jackysoft.service.impl;

import java.util.Collection;
import java.util.List;

import javax.annotation.Nullable;
import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.jackysoft.entity.Folder;
import org.jackysoft.entity.User;
import org.jackysoft.file.FileType;
import org.jackysoft.google.collection.ZTreeNode;
import org.jackysoft.service.AbstractStringIdServiceImpl;
import org.jackysoft.service.FileDataService;
import org.jackysoft.service.FolderService;
import org.jackysoft.util.SpringSecurityUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;

@Service
@Transactional
public class FolderServiceImpl extends AbstractStringIdServiceImpl<Folder> implements
		FolderService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4750245649195426736L;
    private FileDataService fileService;
	@Override
	public void create(Folder folder) throws DataAccessException {
		String owner = SpringSecurityUtils.getCurrentUserName();
		folder.setOwner(owner);
		super.create(folder);
	}

	@Override
	public void remove(Folder folder) throws DataAccessException {
		String hql = "select count(*) from Folder where parent='"
				+ folder.getId() + "'";
		Object o = getSession().createQuery(hql).uniqueResult();
		Long size = o != null ? Long.decode(o.toString()) : 0;
		if (size == 0) {// 没有子文件夹,就删除自己和该文件夹下的所有文件
			hql = "delete from UploadedFileItem where folder='"
					+ folder.getId() + "'";
			getSession().createQuery(hql).executeUpdate();
			getSession().delete(folder);
			
		} else {// 如果有子文件夹,就继续遍历
			hql = "from Folder f where f.parent='" + folder.getId() + "'";
			List<Folder> flist = getSession().createQuery(hql).list();
			for (Folder fd : flist) {
				remove(fd);
			}
		}

	}

	@Override
	public List<Folder> getsbyParent(Folder parent)
			throws DataAccessException {
		Session sess = getSession();
		String owner = SpringSecurityUtils.getCurrentUserName();
		Criteria crt = sess.createCriteria(Folder.class);
		crt.add(Restrictions.eq("parent", parent == null ? "myroot" : parent.getId()));
		crt.add(Restrictions.eq("owner", owner));
		return crt.list();
	}

	@Override
	@Transactional(
			propagation=Propagation.REQUIRES_NEW,
			isolation=Isolation.SERIALIZABLE)
	public void removeByOwner(User owner) throws DataAccessException {
		 log.info("清理用户文件夹....");
		 fileService.remove(owner,FileType.UPLOADED);
		 String hql="delete from Folder f where f.owner=:owner";
		 getSession().createQuery(hql)
		 .setParameter("owner", owner.getUsername())
		 .executeUpdate();
		 log.info("清理用户文件夹完毕....[OK]");
	}

	
	@Resource
	public void setFileService(FileDataService fileService) {
		this.fileService = fileService;
	}

	@Override
	public Collection<ZTreeNode> ztree(Folder bean) throws DataAccessException {
		Collections2.transform(getsbyParent(bean), new Function<Folder,ZTreeNode>(){

			@Override
			public ZTreeNode apply(@Nullable Folder from) {
				ZTreeNode zt = new ZTreeNode();
				zt.setId(from.getId());
				zt.setName(from.getText());
				zt.setIsParent(!from.isLeaf()+"");
				return zt;
			}
			
		});
		return super.ztree(bean);
	}

	
	
	

}
